﻿using System;

namespace HW_c1
{
    public class Node<T>
    {
        public Node<T> next { get; set; }
        public T data { get; set; }
        public Node(T t)
        {
            next = null;
            data = t;
        }
    }
    public class List<T>
    {
        public Node<T> head;
        public Node<T> tail;

        public List()
        {
            head = tail = null;
        }
        public void add(T t)
        {
            Node<T> n = new Node<T>(t);
            if (tail == null)
            {
                tail = n;
                head = n;
            }
            else
            {
                tail.next = n;
                tail = n;
            }
        }
        public void Foreach(Action d)
        {

            d();
        }

    }
    internal class Program
    {
        static public void disp(List<int> L)
        {
            Console.Write("链表的元素分别为:");
            Node<int> p = L.head;
            while (p != null)
            {
                Console.Write("  " + p.data.ToString());
                p = p.next;
            }
            Console.Write("\n");
        }
        static void Main(string[] args)
        {
            List<int> L = new List<int>();
            L.add(2); L.add(3); L.add(4); L.add(6); L.add(8); L.add(7); L.add(7); L.add(17);



            Action d = () => {
                Console.Write("链表的元素分别为:");
                Node<int> p = L.head;
                while (p != null)
                {
                    Console.Write("  " + p.data.ToString());
                    p = p.next;
                }
                Console.Write("\n");
            };
            d += () =>
            {
                Node<int> p = L.head;
                int max = L.head.data;
                int min = L.head.data;
                int sum = 0;
                while (p != null)
                {
                    if (p.data > max) max = p.data;
                    if (p.data < min) min = p.data;
                    sum = sum + p.data; p = p.next;
                }
                Console.WriteLine("链表最大值为:" + max);
                Console.WriteLine("链表最小值为:" + min);
                Console.WriteLine("链表元素之和为:" + sum);
            };
            L.Foreach(d);
        }
    }


}